<?php defined('SYSPATH') or die('No direct script access.');

class Book_Booklet{

// Book_Booklet instances
protected static $_instance;

public static function instance()
{
	if ( ! isset(Book_Booklet::$_instance))
	{
		// Create a new session instance
		Book_Booklet::$_instance = new Book_Booklet();
	}

	return Book_Booklet::$_instance;
}

public function render_thumb($image_file_name)
{
	$thumb_image_path = Kohana::$config->load('upload')->get('thumb_booklet').$image_file_name;
	if (!file_exists($thumb_image_path))
	{
		$booklet_image_path = Kohana::$config->load('upload')->get('booklet').$image_file_name;
		if (!file_exists($booklet_image_path))
		{
			header("Status: 400 书图片不存在");
			exit;
		}
		else
		{
			$this->scare_image($booklet_image_path, $thumb_image_path);
		}
	}
	Image_Output::write($thumb_image_path);
}

public function scare_image($booklet_image_path, $thumb_image_path)
{
	try
	{ 
		$image = Kohana_Image::factory($booklet_image_path);
		$max_width = Kohana::$config->load('upload')->get('max_thumb_width');
		$max_height = Kohana::$config->load('upload')->get('max_thumb_height');
		if ($image->width > $max_width OR  $image->height > $max_height)
		{
			$image->resize($max_width, $max_height, Image::AUTO);
		}
		$image->save($thumb_image_path, 75);
	} 
	catch (Kohana_Exception $e)
	{
		header("Status: 400 书图片错误");
		exit;
	}
}

public function create_booklet_list($array)
{
	$object = array();
	foreach ( $array as $booklet)
	{
		if ($booklet->pictures->count_all() > 0)
		{
			array_push($object,
				array(
					'id'					=> $booklet->id,
					'name'					=> $booklet->name,
					'description'			=> $booklet->description,
					'create_time'			=> $booklet->create_time,
					'download_count'   		=> $booklet->download_count,
					'browse_count'   		=> $booklet->browse_count,
					'picture_count' 		=> $this->get_picture_count($booklet)
				));
		}
	}
	return array(
		'count' => count($object),
		'data'  => $object
	);
}

public function get_picture_count($booklet)
{
	return ORM::factory('picture')->where('booklet_id', '=', $booklet->id)->count_all();
}

public function save_category($booklet, $category_id_array)
{
	if (empty($category_id_array))
	{
		return;
	}
	foreach ($booklet->categories->find_all() as $category)
	{
		$booklet->remove('categories', $category);
	}
	foreach ($category_id_array as $category_id)
	{
		$booklet_category = ORM::factory('booklets_category');
		$booklet_category->booklet_id = $booklet->id;
		$booklet_category->category_id = $category_id;
		$booklet_category->save();
	}
}

public function save_category_by_id($booklet, $category_id)
{
	$booklet_category = ORM::factory('booklets_category');
	$booklet_category->booklet_id = $booklet->id;
	$booklet_category->category_id = $category_id;
	$booklet_category->save();
}

public function save_booklet_record($booklet)
{
	if (!Auth::instance()->logged_in())
	{
		return;
	}
	$booklet_record = ORM::factory('user_booklet');
	$booklet_record->user_id = Auth::instance()->get_user()->id;
	$booklet_record->booklet_id = $booklet->id;
	$booklet_record->create_time = time();
	$booklet_record->save();
}

public function save_image()
{
	if ($_FILES["imgfile"]["error"] > 0)
	{
		return NULL;
	}

	// 图片上传
	try
	{ 
		$image = new Kohana_Image_GD($_FILES['imgfile']['tmp_name']);
		$fileParts = pathinfo($_FILES['imgfile']['name']);
		$image_file_name = md5_file($_FILES['imgfile']['tmp_name']).".".$fileParts['extension'];
		$targetFile =  str_replace('//','/',Kohana::$config->load('upload')->get('booklet')).$image_file_name;
		$image->save($targetFile);
		return $image_file_name;
	} 
	catch (Kohana_Exception $e)
	{
		echo 'Caught exception: ',  $e->getMessage(), "\n";
		return NULL;
	}
}

}



